package cn.neu.leon.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.neu.leon.util.AdverbDict;
import cn.neu.leon.util.EmotionLexicon;
import cn.neu.leon.util.NlpirInit;
import cn.neu.leon.util.Percentage;
import cn.neu.leon.util.Sentiment;
import cn.neu.leon.util.WeiboCrawler;

public class Single extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
		String sInput = request.getParameter("txta");
		String sResult;
		String strEmotion;
		double per[] = new double[7];
		
		EmotionLexicon el = new EmotionLexicon();
		HashMap<String, double[]> em = el.getEmotionMap(); //情感词典读入内存中的Map
		NlpirInit nlpirinit = new NlpirInit();
		
		sResult = nlpirinit.instance.NLPIR_ParagraphProcess(sInput, 1);
		String[] strArray = sResult.split(" ");
		

		for (int i = 0; i < strArray.length; i++) { //遍历每一个分词
			
			// 若存在匹配的表情符号，则该条微博情感值由该表情决定
			if(strArray[i].endsWith("/xm"))
			{
				strEmotion = strArray[i].split("/")[0]; //取出带词性标志分词的中文词
				if(em.containsKey(strEmotion))
				{
					double tempPer[] = em.get(strEmotion);
					for(int j = 0;j<7;j++)
					{
						per[j] = tempPer[j];
					}
					break;
				}
						
			}	
			/*将以下有意义词性的分词取出进行分析*/
			if (strArray[i].endsWith("/v") // 动词 5645
					|| strArray[i].endsWith("/vi")
					|| strArray[i].endsWith("/n")
					|| strArray[i].endsWith("/a")
					|| strArray[i].endsWith("/vl")
					|| strArray[i].endsWith("/vn")
					|| strArray[i].endsWith("/ng")
					|| strArray[i].endsWith("/al")
					|| strArray[i].endsWith("/an")
					|| strArray[i].endsWith("/ng")
					|| strArray[i].endsWith("/nl")
					|| strArray[i].endsWith("/z")
					) {
				strEmotion = strArray[i].split("/")[0]; //取出带词性标志分词的中文词
				/*如果分词在HashMap中存在，则取出value，放入临时数组中*/
				if (em.containsKey(strEmotion)) { 
					double tempPer[] = em.get(strEmotion);
					
					
					int aDegree;
					int aNegative;
					int negNum = 0;
					double degree = 1.0;
					int negative = 0;
					//查情感词所在分句前有没有程度副词，及其位置
					for(int j=i-1;j>=0&&!strArray[j].endsWith("/wd") //逗号
					&&!strArray[j].endsWith("/wf")//分号
					&&!strArray[j].endsWith("/ww")//问号
					&&!strArray[j].endsWith("/wt");j--)//感叹号
					{
						degree = AdverbDict.degree(strArray[j].split("/")[0]);						
						if(degree != 1.0 ) 
						{
							aDegree = j;
							break;
						}
					}
					//查找情感词所在分句前有没有否定副词，及其位置和个数
					for(int k=i-1;k>=0&&!strArray[k].endsWith("/wd") //逗号
					&&!strArray[k].endsWith("/wf")//分号
					&&!strArray[k].endsWith("/ww")//问号
					&&!strArray[k].endsWith("/wt");k--){//感叹号
						negative = AdverbDict.negative(strArray[k].split("/")[0]);
						if(negative == -1)
							{
								aNegative = k;
								negNum++;
							}
					}
					/*不存在否定副词时,情感值乘上程度副词强度，再累加*/
					if(negNum == 0)
					for (int m = 0; m < 7; m++) {
						per[m] += degree*tempPer[m];
					}
					/*存在否定副词时，且个数是奇数,厌恶和喜好，悲伤和高兴交换情感值，再累加*/
					else if(negNum%2 == 1)
					{
						double temp;
						temp = tempPer[0];
						tempPer[0] = tempPer[4];
						tempPer[4] = temp;
						temp = tempPer[1];
						tempPer[1] = tempPer[3];
						tempPer[3] = temp;
						
						for (int m = 0; m < 7; m++) {
							per[m] += degree*tempPer[m];
						}
					}
					else
						for (int m = 0; m < 7; m++) {
							per[m] += degree*tempPer[m];
						}
						
				}
			}

		}

	
			
	        JSONArray series = new JSONArray();
			JSONObject data = new JSONObject();

			for (int i = 0; i < 7; i++) {
				series.add(per[i]);
			}

			data.put("sResult", sResult);
			data.put("series", series);

			String str = data.toString();
			System.out.println(str);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter pw = response.getWriter();
			pw.print(str);
			pw.close();
		

	}

}
